<?php
/*========================================= JooSam =========================================/
/																							/
/	Extention:		JooSam																	/
/	Created by:		Broadcastweb.eu - Paules												/
/	Site:			http://www.broadcastweb.eu												/
/	Email:			support@broadcastweb.eu													/
/	Copyright:		2011 - 2012 (c) Broadcastweb.eu											/
/	License:		CC BY-NC-ND 3.0 ~ http://creativecommons.org/licenses/by-nc-nd/3.0/		/
/	File Version:	0003																	/
/																							/
/========================================== JooSam ========================================*/
// Joomla Security
defined('_JEXEC') or die;

class JooSamHelper{

	public static function Params(){
		
		$app = JFactory::getApplication();
		$params = $app->getParams();

		$global_width = $params->get('jsm_main_width')."%";

		// BUILD BORDERS
		$border_header = $params->get('jsm_header_border_type');
		$border_body = $params->get('jsm_body_border_type');
		if($border_header != 'none'){
			$header_border = $params->get('jsm_header_border_width')."px ".$border_header." ".$params->get('jsm_header_border_color');
			$header_border_radius = $params->get('jsm_header_border_radius')."px";
		} else{
			$header_border = 'none';
			$header_border_radius = "0";
		} if($border_body != 'none'){
			$body_border = $params->get('jsm_body_border_width')."px ".$border_body." ".$params->get('jsm_body_border_color');
			$body_border_radius = $params->get('jsm_body_border_radius')."px";
		} else{
			$body_border = 'none';
			$body_border_radius = "0";
		}

		// SEARCH
		$search_position = $params->get('jsm_search_position');
		if($search_position == 'block'){
			$search_display = 'none';
			$search_float = 'left';
		} else{
			$search_display = 'block';
			$search_float = $search_position;
		}

		//IMAGE
		$xtra_image_width = $params->get('xtra_image_width').'px';
		$xtra_image_height = $params->get('xtra_image_height').'px';

		$config = (object) array(
							'global' => (object) array(
													'width' => $global_width
										),
							'header' => (object) array(
													'background' => $params->get('jsm_header_background'),
													'color' => $params->get('jsm_header_text_color'),
													'border' => $header_border,
													'border_radius' => $header_border_radius
										),
							'body' => (object) array(
													'background' => $params->get('jsm_body_background'),
													'color' => $params->get('jsm_body_text_color'),
													'border' => $body_border,
													'border_radius' => $body_border_radius
										),
							'search' => (object) array(
													'float' => $search_float,
													'display' => $search_display,
													'background' => $params->get('jsm_search_box_background'),
													'color' => $params->get('jsm_search_box_color'),
													'chars' => $params->get('jsm_search_char'),
													'letters_active' => $params->get('jsm_search_letters'),
													'letters_background' => $params->get('jsm_search_letters_background'),
													'letters_background_hover' => $params->get('jsm_search_letters_hover'),
													'letters_color' => $params->get('jsm_search_letters_color'),
													'letters_color_hover' => $params->get('jsm_search_letters_color_hover')
										),
							'table' => (object) array(
													'buttons' => (object) array(
																			'activate_buy' => $params->get('jsm_table_button_buy'),
																			'activate_moreinfo' => $params->get('jsm_table_button_more_info'),
																			'activate_request' => $params->get('jsm_table_button_request'),
																			'background' => $params->get('jsm_table_button_background'),
																			'background_hover' => $params->get('jsm_table_button_hover'),
																			'color' => $params->get('jsm_table_button_color'),
																			'color_hover' => $params->get('jsm_table_button_color_hover')
																),
													'row0_bg' => $params->get('jsm_table_row0_bg'),
													'row0_bg_hover' => $params->get('jsm_table_row0_bg_hover'),
													'row1_bg' => $params->get('jsm_table_row1_bg'),
													'row1_bg_hover' => $params->get('jsm_table_row1_bg_hover'),
													'title_color' => $params->get('jsm_table_txt_title_color'),
													'artist_color' => $params->get('jsm_table_txt_artist_color'),
													'rating' => $params->get('jsm_rating_activate'),
													'rating_color' => $params->get('jsm_rating_color')
										),
							'extrainfo' => (object) array(
													'picture' => $params->get('xtra_info_activate_picture'),
													'title' => $params->get('xtra_info_activate_title'),
													'artist' => $params->get('xtra_info_activate_artist'),
													'songtext' => $params->get('xtra_info_activate_songtext'),
													'dateplayed' => $params->get('xtra_info_activate_dateplayed'),
													'rating' => $params->get('xtra_info_activate_rating'),
													'descr' => $params->get('xtra_info_activate_descr'),
													'duration' => $params->get('xtra_info_activate_duration'),
													'buybutton' => $params->get('xtra_info_activate_buybutton'),
													'requestbutton' => $params->get('xtra_info_activate_requestbutton'),
													'image_width' => $xtra_image_width,
													'image_height' => $xtra_image_height
										)
					);
		return $config;
	}

	public static function config(){

		$db =& JFactory::getDBO();
		$params = JooSamHelper::Params();
		$conf_query = "	SELECT * FROM ".$db->nameQuote('#__joosam_server')." WHERE ".$db->nameQuote('srv_active')." = 1";
		$db->setQuery($conf_query);
		$conf = $db->loadObject();
		if($conf){
			$error = false;
			$timezone = date_default_timezone_get();
			$srv_id = $conf->id;
			$srv_name = $conf->srv_name;
			$srv_description = $conf->srv_descr;
			$srv_sqlip = $conf->sql_ip;
			$srv_sqlport = $conf->sql_port;
			$srv_sqluser = $conf->sql_user;
			$srv_sqlpass = JooSamHelper::jsmCrypt('decrypt', $conf->sql_pass);
			$srv_sqldb = $conf->sql_db;
			$srv_admin = $conf->set_admin;
			$srv_active = $conf->srv_active;
			$srv_samip = $conf->sam_ip;
			$srv_samport = $conf->sam_port;
			$srv_imagetype = $conf->image_type;
			$srv_imagedata = $conf->image_data;
			$srv_timezone = $conf->sam_timezone;
			// Get Timezone (php 5.1.0+)
			if($timezone != $srv_timezone){ date_default_timezone_set($srv_timezone); }
			$time = date('H:i:s');
			$date = date('Y-m-d');
			$sql_daycol = 'evt_day_'.strtolower(date('D'));

			// Query - 1 time event running
			$req1_query = "	SELECT *
							FROM ".$db->nameQuote('#__joosam_evtreq')."
							WHERE ".$db->nameQuote('srv_id')." = ".$db->quote($srv_id)."
								AND ".$db->nameQuote('evt_time_start')." < ".$db->quote($time)."
								AND ".$db->nameQuote('evt_time_end')." > ".$db->quote($time)."
								AND ".$db->nameQuote('auto_update')." = ".$db->quote('0')."
								AND ".$db->nameQuote('evt_date')." = ".$db->quote($date)."
						";
			$db->setQuery($req1_query);
			$ReqEvt = $db->loadObject();

			// Query - Event Day Running
			if(!$ReqEvt){

				$req2_query = "	SELECT *
								FROM ".$db->nameQuote('#__joosam_evtreq')."
								WHERE ".$db->nameQuote('srv_id')." = ".$db->quote($srv_id)."
									AND ".$db->nameQuote('evt_time_start')." < ".$db->quote($time)."
									AND ".$db->nameQuote('evt_time_end')." > ".$db->quote($time)."
									AND ".$db->nameQuote('auto_update')." = ".$db->quote('1')."
									AND ".$db->nameQuote($sql_daycol)." = ".$db->quote('1')."
							";
				$db->setQuery($req2_query);
				$ReqEvt = $db->loadObject();

				// Query - Load Default Data
				if(!$ReqEvt){

					$req3_query = "	SELECT *
									FROM ".$db->nameQuote('#__joosam_evtreq')."
									WHERE ".$db->nameQuote('srv_id')." = ".$db->quote($srv_id)."
										AND ".$db->nameQuote('auto_update')." = ".$db->quote('2')."
								";
					$db->setQuery($req3_query);
					$ReqEvt = $db->loadObject();
					
					if(!$ReqEvt){
						$evtreq = false;
					} else{
						$evtreq = $ReqEvt;
					}
				}
				else{
					$evtreq = $ReqEvt;
				}
			}
			else{
				$evtreq = $ReqEvt;
			}
			if($evtreq){
				$data_id = $evtreq->id;
				$evt_time_start = $evtreq->evt_time_start;
				$evt_time_end = $evtreq->evt_time_end;
				$evt_day_mon = $evtreq->evt_day_mon;
				$evt_day_tue = $evtreq->evt_day_tue;
				$evt_day_wed = $evtreq->evt_day_wed;
				$evt_day_thu = $evtreq->evt_day_thu;
				$evt_day_fri = $evtreq->evt_day_fri;
				$evt_day_sat = $evtreq->evt_day_sat;
				$evt_day_sun = $evtreq->evt_day_sun;
				$evt_date = $evtreq->evt_date;
				$evt_output_title = $evtreq->evt_output_title;
				$evt_output_content = $evtreq->evt_output_content;
				$evt_name = $evtreq->evt_name;
				$evt_description = $evtreq->evt_description;
				$auto_update = $evtreq->auto_update;
				$view_songtype = $evtreq->view_songtype;
				$view_categories = $evtreq->view_categories;
				$accapt_categories = $evtreq->accapt_categories;
				$front_req_text = $evtreq->front_req_text;
				$request_msg = $evtreq->request_msg;
				$more_info_html = $evtreq->more_info_html;
			}
			else{
				$data_id = 0;
				$evt_time_start = '';
				$evt_time_end = '';
				$evt_day_mon = '';
				$evt_day_tue = '';
				$evt_day_wed = '';
				$evt_day_thu = '';
				$evt_day_fri = '';
				$evt_day_sat = '';
				$evt_day_sun = '';
				$evt_date = '';
				$evt_output_title = '';
				$evt_output_content = '';
				$evt_name = '';
				$evt_description = '';
				$auto_update = '';
				$view_songtype = '';
				$view_categories = '';
				$accapt_categories = '';
				$front_req_text = '';
				$request_msg = '';
				$more_info_html = '';
			}
			
			// Text strings
			$txt_query = "	SELECT *
							FROM ".$db->nameQuote('#__joosam_textstrings')."
							WHERE ".$db->nameQuote('srv_id')." = ".$db->quote($srv_id)."
						";
			$db->setQuery($txt_query);
			$text = $db->loadObjectList();
			$txt = array();
			if($text){
				foreach($text as $str){
					$code = $str->txt_code;
					$txt[$code] = JText::_($str->txt_string);
				}
			}

			// Default Text Load When No Text Is Given
			if(array_key_exists(200, $txt)){$txt[200] = JText::_($txt[200]);} else{ $txt[200] = JText::_('COM_JOOSAM_RESPONSE_TXT_200'); }			
			if(array_key_exists(500, $txt)){$txt[500] = JText::_($txt[500]);} else{ $txt[500] = JText::_('COM_JOOSAM_RESPONSE_TXT_500'); }
			if(array_key_exists(501, $txt)){$txt[501] = JText::_($txt[501]);} else{ $txt[501] = JText::_('COM_JOOSAM_RESPONSE_TXT_501'); }
			if(array_key_exists(502, $txt)){$txt[502] = JText::_($txt[502]);} else{ $txt[502] = JText::_('COM_JOOSAM_RESPONSE_TXT_502'); }
			if(array_key_exists(503, $txt)){$txt[503] = JText::_($txt[503]);} else{ $txt[503] = JText::_('COM_JOOSAM_RESPONSE_TXT_503'); }			
			if(array_key_exists(600, $txt)){$txt[600] = JText::_($txt[600]);} else{ $txt[600] = JText::_('COM_JOOSAM_RESPONSE_TXT_600'); }
			if(array_key_exists(601, $txt)){$txt[601] = JText::_($txt[601]);} else{ $txt[601] = JText::_('COM_JOOSAM_RESPONSE_TXT_601'); }
			if(array_key_exists(602, $txt)){$txt[602] = JText::_($txt[602]);} else{ $txt[602] = JText::_('COM_JOOSAM_RESPONSE_TXT_602'); }
			if(array_key_exists(603, $txt)){$txt[603] = JText::_($txt[603]);} else{ $txt[603] = JText::_('COM_JOOSAM_RESPONSE_TXT_603'); }
			if(array_key_exists(604, $txt)){$txt[604] = JText::_($txt[604]);} else{ $txt[604] = JText::_('COM_JOOSAM_RESPONSE_TXT_604'); }
			if(array_key_exists(605, $txt)){$txt[605] = JText::_($txt[605]);} else{ $txt[605] = JText::_('COM_JOOSAM_RESPONSE_TXT_605'); }
			if(array_key_exists(606, $txt)){$txt[606] = JText::_($txt[606]);} else{ $txt[606] = JText::_('COM_JOOSAM_RESPONSE_TXT_606'); }
			if(array_key_exists(609, $txt)){$txt[609] = JText::_($txt[609]);} else{ $txt[609] = JText::_('COM_JOOSAM_RESPONSE_TXT_609'); }
			if(array_key_exists(610, $txt)){$txt[610] = JText::_($txt[610]);} else{ $txt[610] = JText::_('COM_JOOSAM_RESPONSE_TXT_610'); }
			if(array_key_exists(700, $txt)){$txt[700] = JText::_($txt[700]);} else{ $txt[700] = JText::_('COM_JOOSAM_RESPONSE_TXT_700'); }
			if(array_key_exists(701, $txt)){$txt[701] = JText::_($txt[701]);} else{ $txt[701] = JText::_('COM_JOOSAM_RESPONSE_TXT_701'); }
			if(array_key_exists(702, $txt)){$txt[702] = JText::_($txt[702]);} else{ $txt[702] = JText::_('COM_JOOSAM_RESPONSE_TXT_702'); }
			if(array_key_exists(703, $txt)){$txt[703] = JText::_($txt[703]);} else{ $txt[703] = JText::_('COM_JOOSAM_RESPONSE_TXT_703'); }
			if(array_key_exists(704, $txt)){$txt[704] = JText::_($txt[704]);} else{ $txt[704] = JText::_('COM_JOOSAM_RESPONSE_TXT_704'); }
			if(array_key_exists(705, $txt)){$txt[705] = JText::_($txt[705]);} else{ $txt[705] = JText::_('COM_JOOSAM_RESPONSE_TXT_705'); }
			if(array_key_exists(706, $txt)){$txt[706] = JText::_($txt[706]);} else{ $txt[706] = JText::_('COM_JOOSAM_RESPONSE_TXT_706'); }
			if(array_key_exists(707, $txt)){$txt[707] = JText::_($txt[707]);} else{ $txt[707] = JText::_('COM_JOOSAM_RESPONSE_TXT_707'); }
			if(array_key_exists(708, $txt)){$txt[708] = JText::_($txt[708]);} else{ $txt[708] = JText::_('COM_JOOSAM_RESPONSE_TXT_708'); }
		}
		else{
			$error = true;
		}

		// BUILD OUTPUT
		if(!$error){
			$result = (object) array(
									'sql' => (object) array(
														'ip' => $srv_sqlip,
														'port' => $srv_sqlport,
														'user' => $srv_sqluser,
														'pass' => $srv_sqlpass,
														'db' => $srv_sqldb
														),
									'sam' => (object) array(
														'ip' => $srv_samip,
														'port' => $srv_samport
														),
									'image' => (object) array(
														'source' => $srv_imagetype,
														'width' => $params->extrainfo->image_width,
														'height' => $params->extrainfo->image_height,
														'data' => $srv_imagedata
														),
									'server' => (object) array(
														'id' => $srv_id,
														'name' => $srv_name,
														'description' => $srv_description,
														'admin' => $srv_admin,
														'active' => $srv_active,
														'timezone' => $srv_timezone
														),
									'event' => (object) array(
														'name' => $evt_name,
														'description' => $evt_description,
														'date' => $evt_date,
														'time_start' => $evt_time_start,
														'time_end' => $evt_time_end,
														'module_title' => $evt_output_title,
														'module_content' => $evt_output_content,
														'update' => $auto_update,
														'days' => (object) array(
																				'monday' => $evt_day_mon,
																				'tuesday' => $evt_day_tue,
																				'wednesday' => $evt_day_wed,
																				'thursday' => $evt_day_thu,
																				'friday' => $evt_day_fri,
																				'saterday' => $evt_day_sat,
																				'sunday' => $evt_day_sun
																			)
														),
									'request' => (object) array(
															'message' => $request_msg,
															'songtype' => $view_songtype,
															'category_view' => $view_categories,
															'category_accapt' => $accapt_categories,
															'front_text' => $front_req_text,
															'song_info_layout' => $more_info_html
														),
									'txt' => $txt
								);
		}
		else{
			$result = (object) array(
									'error' => 'error'
								);
		}
		return $result;
	}

	public static function jsmCrypt($type, $string){

		$jConf = JFactory::getApplication();
		$key = $jConf->getCfg('secret');
		$key = md5($key);
		$password = $string;
		if(function_exists('mcrypt_module_open')){
			$cipher = MCRYPT_RIJNDAEL_256;
			$mode = MCRYPT_MODE_CBC;
			$iv = md5($key);
			$encrypt = base64_encode(mcrypt_encrypt($cipher, $key, $password, $mode, $iv));
			$decrypt = rtrim(mcrypt_decrypt($cipher, $key, base64_decode($password), $mode, $iv));
		}
		else{
			$encrypt = base64_encode($password);
			$decrypt = base64_decode($password);
		}

		if($type == 'decrypt'){ return $decrypt; }
		else if($type == 'encrypt'){ return $encrypt; }
		else{ return false; }
	}
}